package com.neusoft.lohas.common.pay.upowp;

import javax.servlet.http.HttpServlet;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import com.Ostermiller.util.MD5;
import com.neusoft.lohas.common.pay.upowp.util.DesUtil2;
import com.neusoft.lohas.common.pay.upowp.util.EncDecUtil;
import com.neusoft.lohas.common.pay.upowp.util.RSACoder;
import com.neusoft.lohas.common.pay.upowp.util.RemoteAccessor;

public class PreOrder extends HttpServlet {

	public static void main(String[] args) {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
		java.util.Date currentTime = new java.util.Date();//得到当前系统时间 
		String pDate = formatter.format(currentTime);
		
		RemoteAccessor remoteAccessor = new RemoteAccessor();
    	try{
	    	String data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
	    			"<upbp application=\"MGw.Req\" version =\"1.0.0\" sendTime =\""+pDate+"\" sendSeqId =\"00000888888000\">" +
	    			"<frontUrl>http://www.163.com</frontUrl><merchantOrderDesc></merchantOrderDesc><misc></misc>" +
	    			"<gwType>01</gwType><transTimeout>ssssss</transTimeout><backUrl>http://192.168.1.156:8080/Test/Notify</backUrl>" +
	    			"<merchantOrderCurrency>156</merchantOrderCurrency><merchantOrderAmt>1</merchantOrderAmt>" +
	    			"<merchantOrderDesc>呵呵</merchantOrderDesc>"+
	    			"<merchantId>802310048993225</merchantId><merchantOrderTime>"+pDate+"</merchantOrderTime>" +
	    			"<merchantOrderId>"+pDate+"</merchantOrderId><merchantUserId></merchantUserId>" +
	    			"<mobileNum>15388157741</mobileNum>" +
	    			"<cardNum></cardNum></upbp>";
	    	BASE64Encoder encoder = new BASE64Encoder();
	    	BASE64Decoder decoder = new BASE64Decoder();
	    	String merchantId = encoder.encodeBuffer("802310048993225".getBytes());
	    	String mm = MD5.getHashString("654321");
	    	String keyPath = "D:/Jackie/SHFZY/maygo/someworld/src/main/webapp/unionpay/802310048993225.pfx";
	    	String privateKey = EncDecUtil.getCertKey("123456",keyPath);
	    	String desKey = encoder.encodeBuffer(RSACoder.encryptByPrivateKey(mm.getBytes(), decoder.decodeBuffer(privateKey)));
	    	byte[] bodyb = DesUtil2.encrypt(data.getBytes("utf-8"), mm.getBytes());
	    	String miBody = encoder.encode(bodyb);
	    	String xml = merchantId + "|" + desKey + "|" + miBody;
	    	String re = remoteAccessor.getResponseByStream("http://upwap.bypay.cn/gateWay/gate.html", "utf-8", xml);
	    	System.out.println("返回结果："+re);
	    	String[] strArr = re.split("\\|");
	    	byte[] de = decoder.decodeBuffer(strArr[1]);
	    	byte[] b = DesUtil2.decrypt(de, mm.getBytes());
	    	String content = new String(b,"utf-8");
	    	System.out.println(content);
	    	Document document = DocumentHelper.parseText(content);
	    	Element upbp = document.getRootElement();
	    	String redirectPage = upbp.elementText("gwInvokeCmd");
	    	System.out.println("重定向地址：");
	    	System.out.println(redirectPage);
    	}catch (Exception e) {
    		e.printStackTrace();
    	}
    }
}
